package com.backstage.service.impl;

import com.backstage.common.annotation.transaction.Commit;
import com.backstage.common.annotation.transaction.ReadOnly;
import com.backstage.common.page.Page;
import com.backstage.common.uitil.DES_3;
import com.backstage.common.uitil.StringUtil;
import com.backstage.dao.UserDao;
import com.backstage.entity.Role;
import com.backstage.entity.User;
import com.backstage.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Project authority
 * @Package com.backstage.service
 * @Data 2017/7/11下午 1:32.
 * @Aurhor 阮雪峰
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Commit
    @Override
    public void updatePassword(User loginUser) {
        userDao.updatePassword(loginUser);
    }

    @ReadOnly
    @Override
    public void userByPage(Page<User> page, User user) {
        int totalCol = userDao.getTotalColumn(page, user);
        List<User> list = userDao.getList(page, user);

        page.setTotal(totalCol);
        page.setRows(list);
    }

    @Commit
    @Override
    public void add(User user) {
        user.setDes_key(StringUtil.getRandomStr(32));
        user.setPassword(DES_3.encryptBy3DES(user.getUser_name(), user.getDes_key()));
        userDao.add(user);
        if(user.getRoles() != null && user.getRoles().size() > 0) {
            userDao.relateRoles(user);
        }
    }


    @Commit
    @Override
    public void edit(User user) {
        userDao.edit(user);
        userDao.deleteRelateRoles(user);
        if(user.getRoles() != null && user.getRoles().size() > 0){
            userDao.relateRoles(user);
        }
    }

    @Commit
    @Override
    public void delete(ArrayList<User> users) {
        userDao.deleteBach(users);
        userDao.deleteRelateRolesBach(users);
    }

    @ReadOnly
    @Override
    public User getUserById(User user) {
        User u = userDao.getObjById(user);
        u.setPassword(null);
        u.setDes_key(null);
        return u;
    }

    @Commit
    @Override
    public void updateStatus(User user) {
        if(1 == user.getStatus()){
            user.setStatus(0);
        }else{
            user.setStatus(1);
        }
        userDao.updateStatus(user);
    }

    @ReadOnly
    @Override
    public boolean isExistUserName(User user) {
        return (userDao.isExistUserName(user) > 0);
    }

    @ReadOnly
    @Override
    public List<Role> getAllRoles() {
        return userDao.getAllRoles();
    }

    @Override
    public void initPassword(User user) {
        User u = userDao.getObjById(user);
        user.setPassword(DES_3.encryptBy3DES(u.getUser_name(), u.getDes_key()));
        user.setDes_key(u.getDes_key());

        userDao.updatePassword(user);
    }
}
